package cn.xfdzx.android.apps.shop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.adapter.SearchShopsAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpFragment;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.HomeRetailLevelContract;
import cn.xfdzx.android.apps.shop.evenbus.HomeTypeDataMessage;
import cn.xfdzx.android.apps.shop.presenter.HomeRetailLevelPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.UtilsData;

/**
 * @author gzk
 * @date 2020/3/26 0026
 * description：首页分类下
 */
public class RetailLevelFragment extends BaseMvpFragment<HomeRetailLevelPresenter> implements HomeRetailLevelContract.View {


    RecyclerView rvShop;
    SmartRefreshLayout mRefreshLayout;
    LinearLayout llHint;


    private int type = 0;//类别 0-零售 1-批发
    private int page = 1;
    private int limit = Constants.LIMIT;//每页多少条
    private int sid = 0;
    private String keyWrod = "";
    private String is_store = "1";//是否查询店铺，1查询
    private String priceOrder = "";
    private String salesOrder = "";
    private List<HomeSearchXFDGoodBeen.DataBean> mList = new ArrayList<>();
    private SearchShopsAdapter searchShopsAdapter;
    private String user_curent_lng,user_curent_lat;//首页经纬度

    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);
        rvShop=view.findViewById(R.id.rv_shop);
        mRefreshLayout=view.findViewById(R.id.refreshLayout);
        llHint=view.findViewById(R.id.ll_hint);
        mPresenter = new HomeRetailLevelPresenter();
        mPresenter.attachView(this);

        sid = getArguments().getInt("sid");
        user_curent_lng = UtilsData.getPreference(getActivity(), "coordinates").getString("coordinates_lng");
        user_curent_lat = UtilsData.getPreference(getActivity(), "coordinates").getString("coordinates_lat");

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvShop.setLayoutManager(linearLayoutManager);
        searchShopsAdapter = new SearchShopsAdapter(getActivity(), mList, type);
        searchShopsAdapter.setOnItemClick(new SearchShopsAdapter.OnItemClick() {
            @Override
            public void setOnItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), ShopActivity.class);
                intent.putExtra("shop_id", mList.get(position).getId() + "");
                startActivity(intent);
            }
        });
        rvShop.setAdapter(searchShopsAdapter);

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.initSearchData(true, true, user_curent_lng,user_curent_lat,sid+"", page + "", limit + "", type + "", is_store, "", keyWrod, priceOrder, salesOrder, "");

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(HomeTypeDataMessage messageEvent) {

        if (messageEvent.getMessage() == sid) {
            page = 1;
            mPresenter.initSearchData(true, true, user_curent_lng,user_curent_lat,sid+"", page + "", limit + "", type + "", is_store, "", keyWrod, priceOrder, salesOrder, "");

        }
    }
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop;
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()){
            ProgressDialog.getInstance().show(getActivity());
        }
    }


    @Override
    public void hideLoading() {
        if (ProgressDialog.getInstance().materialDialog.isShowing()){
            ProgressDialog.getInstance().dismiss();
        }
    }


    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onInitSearchDataSuccess(HomeSearchXFDGoodBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page==1){
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initSearchData(false,false,user_curent_lng,user_curent_lat,sid+"", page + "", limit + "", type + "", is_store, "", keyWrod,  priceOrder, salesOrder, "");

                    }
                });
            }
            if (bean.getData().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
                mList.addAll(bean.getData());
                searchShopsAdapter.notifyDataSetChanged();
            }
            if (mList.size()==0){
                llHint.setVisibility(View.VISIBLE);
            }else {
                llHint.setVisibility(View.GONE);
            }
        }else {
            Toast.makeText(getActivity(), bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);
    }


    public static RetailLevelFragment newInstance(int userUid) {
        Bundle bundle = new Bundle();
        bundle.putInt("sid",userUid);
        RetailLevelFragment fragment = new RetailLevelFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();

    }

    @Override
    public void onDestroy() {
        mList.clear();
        mList = null;
        super.onDestroy();
    }

}
